﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Praxis.Entities;
using Praxis.BusinessLogic;

namespace Praxis.UI
{
    public partial class FrmCliente : Form
    {
        [Dependency]
        public IClienteBL clienteBL { get; set; }


        public FrmCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmCliente_Load(object sender, EventArgs e)
        {
            Cargar();
        }

        private void Cargar()
        {
            cLIENTEBindingSource.DataSource = clienteBL.GetAllClientes();
        }

        private void BtnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoCliente frmmttocliente = new FrmMttoCliente();
            frmmttocliente.Nuevo();
            frmmttocliente.ShowDialog();
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            CLIENTE currentCliente = cLIENTEBindingSource.Current as CLIENTE;
            if (currentCliente != null)
            {
                FrmMttoCliente frmmttocliente = new FrmMttoCliente();
                frmmttocliente.CurrentCliente = currentCliente;
                frmmttocliente.Editar();
                frmmttocliente.ShowDialog();
            }
            else
            {
                MessageBox.Show("Mensaje", "Seleccione un Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            if (rbNomRazSocial.Checked)
                cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteByDescripcion(TxtDescripcion.Text);

            else
                if (rbRucDni.Checked)
                    cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteByRuc(TxtDescripcion.Text);
        
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbNomRazSocial.Checked)
                    cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteByDescripcion(TxtDescripcion.Text);

                else
                    if (rbRucDni.Checked)
                        cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteByRuc(TxtDescripcion.Text);
            }
        }

        private void cLIENTEDataGridView_Click(object sender, EventArgs e)
        {
            
        }

        private void cLIENTEDataGridView_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }

        public CLIENTE CurrentCliente { get; set; }

        private void cLIENTEDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CurrentCliente = cLIENTEBindingSource.Current as CLIENTE;
        }

        private void rbNomRazSocial_CheckedChanged(object sender, EventArgs e)
        {
            if (rbNomRazSocial.Checked)
            {
                TxtDescripcion.Text = "";
                TxtDescripcion.MaxLength = 50;
                Cargar();
            }
            else if (rbRucDni.Checked)
            {
                TxtDescripcion.Text = "";
                TxtDescripcion.MaxLength = 11;
                Cargar();
            }
        }

        private void TxtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbNomRazSocial.Checked)
            {
                if (Char.IsLetter(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (rbRucDni.Checked)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

    }
}
